package com.zhang.authoritycenter.common.generator;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 逆向工程生成的entity实体类中id注解为auto，需要修改为input
 * @author zfj
 * @date 2022/3/31
 */
public class CodeGenerator {
    private static String url = "jdbc:mysql://10.88.130.97:3308/business_authority_center?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
//    private static String url = "jdbc:mysql://10.88.130.97:3308/business_product_center?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
    private static String driverName = "com.mysql.cj.jdbc.Driver";
    private static String username = "root";
    private static String password = "root";
    private static String file_path = "D:/code/business-center";

    /**
     * 表名，多个英文逗号分割，如果是全表，就为null
     * */
    private static String[] tableName = new String[]{"user_base","open_account"};

    private static final String entityPath = "com.zhang.authority";
    private static final String xmlPah = "/src/main/resources/mapper";

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        //外层包名
        String packageName = "com";
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // String projectPath = "D:/project"; //windows下生成文件的位置
        String projectPath = file_path;

        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setFileOverride(true);
        //生成文件的作者名称
        gc.setAuthor("KingHood");
        gc.setOpen(false);
        gc.setActiveRecord(true);
        //gc.setEnableCache(true);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setDateType(DateType.ONLY_DATE);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
         dsc.setDriverName(driverName);
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName("core");
        pc.setParent(entityPath);
        pc.setServiceImpl("service");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + xmlPah + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        if(Objects.nonNull(tableName)){
            strategy.setInclude(tableName);
        }
        strategy.setEntityTableFieldAnnotationEnable(true);
        strategy.setTablePrefix("");

        strategy.entityTableFieldAnnotationEnable(true);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
